import os
import re
import location
import culinary
import foodtime

def test_loc(test_number, answer):
    body_text = get_body_text(test_number)
    location_str = location.location_of_food(body_text, location.buildings)
    if (answer == location_str):
        print "[PASS] Test %d" % test_number
    else:
        print "[FAIL] Test %d" % test_number
    print location_str
    
def test_food(test_number, answer):
    body_text = get_body_text(test_number)
    food_status = culinary.check_free_food(body_text)
    if (answer == food_status[0]):
        print "[PASS] Test %d" % test_number
    else:
        print "[FAIL] Test %d" % test_number    
    print food_status
    
def test_time(test_number):
    body_text = get_body_text(test_number)
    food_time = foodtime.get_food_time(body_text)
    print 'TEST: %s GOT TIME: %s' % (test_number, food_time)
    
def input_email_clean():
    for x in range(0,12):
        body_text = get_body_text(x)
        if body_text:
            addr_cleaner = re.compile('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}', re.I)
            body_text = addr_cleaner.sub('email@removed.com',body_text)  
            email_name = "inputs/email" + str(x) + ".txt"
            f = open(email_name, "w")
            f.write(body_text)
            f.close()
def get_body_text(test_number):
    email_name = "inputs/email" + str(test_number) + ".txt"
    if not os.path.exists(email_name):
        print "[SETUP FAILURE] " + email_name + " does not exist"
        return None
    print "Testing " + email_name
    f = open(email_name, "r")
    body_text = f.read()
    f.close()
    return body_text

def location_driver():
    print "Testing Location Extraction"
    test_loc(1, "Outside 3725")
    test_loc(2, "CSE 3725")
    test_loc(3, "LBME Atrium (1101 Beal Avenue)")
    test_loc(4, "3725CSE")
    test_loc(5, "3725 CSE")
    test_loc(6, "1255 North Quad, Central Campus")
    test_loc(7, "3rd floor elevators")
    test_loc(8, "3649")
    test_loc(9, "3725 CSE")
    test_loc(10, "3rd floor alcove area")
    test_loc(11, 'LBME Atrium (1101 Beal Avenue)')
    
    
def food_driver():
    print "Testing Food Extraction"
    test_food(1, True)
    test_food(2, True)
    test_food(3, True)
    test_food(4, True)
    test_food(5, True)
    test_food(6, True)
    test_food(7, True)
    test_food(8, True)
    test_food(9, True)
    test_food(10, True)
    test_food(11, True)
    
def time_driver():
    print "Testing time extraction"
    for x in range(1,12):
        test_time(x)
    #test_time(11)
if __name__ == "__main__":
    #location_driver()	
    #food_driver()
    time_driver()
    #input_email_clean()